﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using MicroLibraryAssembly;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.Data;
using System.Web.Mail;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Data.SqlClient;
/// <summary>
/// Summary description for User
/// </summary>
public class clsUser
{
    #region Property
    public int ID { get; set; }
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
    public string Mobile { get; set; }
    public string City { get; set; }
    public string Zipcode { get; set; }
    public string Country { get; set; }
    public string State { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }
    public bool IsActive { get; set; }
    public int UserType { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }
    public string ImageName { get; set; }
    public int CreatedByUserID { get; set; }
    public DateTime CreatedDate { get; set; }
    public DateTime DateOfBirth { get; set; }
    public DateTime EditedOn { get; set; }
    public DateTime LastLoggedOn { get; set; }
    public int EditedBy { get; set; }
    public string Gender { get; set; }
    #endregion
    public SqlDataReader objDataReader;
    public clsUser()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public bool PasswordChange( int ID,string strUserName, string strPassword, string strPageCode,string strOldPassword, int userType)
    {
        bool status = false;
        SqlParameter[] objParameter = new SqlParameter[6];
        try
        {
            objParameter[0] = new SqlParameter("@UsernameOrEmail", strUserName);
            objParameter[1] = new SqlParameter("@PASSWORD", strPassword);
            objParameter[2] = new SqlParameter("@PAGECODE", strPageCode);
            objParameter[3] = new SqlParameter("@OLD_PASSWORD", strOldPassword);
            objParameter[4] = new SqlParameter("@ID", ID);
            objParameter[5] = new SqlParameter("@userType", userType);
            status = Convert.ToBoolean(SqlHelper.ExecuteNonQuery(ConfigClass.DbConn, CommandType.StoredProcedure,
                 "P_CHANGE_PASSWORD", objParameter));
        }
        catch (Exception ex) 
        {
        
        }
        finally
        {
            objParameter = null;
        }
        return status;
    }


    public SqlDataReader CheckLogin(string strUserName, string strPassword, string strEmail,string pagecode)
    {
        SqlParameter[] objParameter = new SqlParameter[4];
        try
        {
            objParameter[0] = new SqlParameter("@UserName", strUserName);
            objParameter[1] = new SqlParameter("@PASSWORD", strPassword);
            objParameter[2] = new SqlParameter("@EMAIL", strEmail);
            objParameter[3] = new SqlParameter("@pagecode", pagecode);
            objDataReader = SqlHelper.ExecuteReader(ConfigClass.DbConn,
                "P_CHECK_LOGIN", objParameter);
        }
        catch (Exception ex)
        { 
        
        }
        finally
        {
            objParameter = null;
        }
        return objDataReader;
    }

    public Int32 SendMail(String To, String From, String Subject, String Body)
    {
        try
        {
            MailMessage msgMail = new MailMessage();
            msgMail.To = To;
            SmtpMail.SmtpServer = "mail.google.com";
            msgMail.From = From;
            msgMail.Subject = Subject;
            msgMail.BodyFormat = MailFormat.Html;
            msgMail.Body = Body;
            SmtpMail.Send(msgMail);
            return 1;
        }
        catch (Exception)
        {

            return -1;
        }

    }
    public static string ReadFileReturnHTML(string dirPath, string fileName, Hashtable keywordsWithValues)
    {
        string HTMLtext = string.Empty;
        try
        {
            if (fileName == string.Empty)
            {
                throw new ArgumentNullException("Filename found empty");
            }


            string dirAbsPath = MakeDirPath(dirPath);
            if (File.Exists(dirAbsPath + fileName))
            {
                HTMLtext = File.ReadAllText(dirAbsPath + fileName);
            }
            foreach (DictionaryEntry keyValue in keywordsWithValues)
            {
                HTMLtext = HTMLtext.Replace(keyValue.Key.ToString(), keyValue.Value.ToString());
            }

        }
        catch (Exception exception)
        {
            //ExceptionHandlingClass.HandleException(exception);
        }

        return HTMLtext;

    }
    private static string MakeDirPath(string dirPath)
    {
        if (dirPath == null)
        {
            dirPath = string.Empty;
        }
        dirPath = dirPath.Trim();
        dirPath = dirPath.Replace(@"\", "/");
        if ((dirPath != string.Empty) && (dirPath.IndexOf("/") != 0))
        {
            dirPath = "/" + dirPath;
        }
        string str = HttpContext.Current.Server.MapPath(HttpContext.Current.Request.ApplicationPath + dirPath);
        if (str.Substring(str.Length - 1, 1) != @"\")
        {
            str = str + @"\";
        }
        return str;
    }
    public  SqlDataReader GetDetails(string strPageCode, string parameter2)
    {
        SqlDataReader objDataReader = null;
        SqlParameter[] objParameter = new SqlParameter[2];
        objParameter[0] = new SqlParameter("@PAGE_CODE", strPageCode);
        objParameter[1] = new SqlParameter("@parameter2", parameter2);


        try
        {
            objDataReader = SqlHelper.ExecuteReader(ConfigClass.DbConn, CommandType.StoredProcedure,
                                          "P_GET_DETAIL", objParameter);
        }
        catch (Exception ex)
        {
            //ExceptionHandlingClass.HandleException(ex);
        }
        finally
        {
            objParameter = null;
        }
        return objDataReader;
    }
}